package com.example.travelservlet.commentEntertainment.service;

import com.example.travelservlet.commentEntertainment.dao.CommentMapper;
import com.example.travelservlet.entertainment.repository.EntertainmentRepository;
import com.example.travelservlet.entity.Comment;
import com.example.travelservlet.entity.CommentDto;
import com.example.travelservlet.entity.Entertainment;
import com.example.travelservlet.entity.User;
import com.example.travelservlet.user.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author hxy
 * @date 2023/5/16 9:04
 */
@Service
public class CommentService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private EntertainmentRepository entertainmentRepository;

    public Integer insertComment(Comment comment){
        return commentMapper.insertComment(comment);
    }

    public List<CommentDto> findCommentByEntertainmentId(String entertainmentId){
        List<Comment> commentList = commentMapper.findCommentByEntertainmentId(entertainmentId);

        List<CommentDto> commentDtoList = new ArrayList<>();
        for (Comment comment : commentList){
            CommentDto commentDto = new CommentDto();
            Optional<User> optionalUser = userRepository.findById(comment.getUserId());
            Optional<Entertainment> optionalEntertainment = entertainmentRepository.findById(comment.getEntertainmentId());
            User user = optionalUser.get();
            Entertainment entertainment = optionalEntertainment.get();
            commentDto.setUser(user);
            commentDto.setEntertainment(entertainment);
            BeanUtils.copyProperties(comment,commentDto);
            commentDtoList.add(commentDto);
        }
        return commentDtoList;
    }
    public Comment findById(String id){
        return commentMapper.findCommentById(id);
    }

    public void deleteCommentById(String id) {
       commentMapper.deleteCommentById(id);
    }

    public List<Comment> findCommentByKeyWord(String key){
        List<Comment> comments = commentMapper.findCommentByKeyWord(key);
        List<Comment> commentList = new ArrayList<>();
        for (Comment comment:comments){
            comment.setEntertainmentId(entertainmentRepository.findEntertainmentByEntertainmentId(comment.getEntertainmentId()).getName());
            commentList.add(comment);
        }
        return commentList;
    }
    public List<Comment> findCommentByKeyWord1(String key,Integer page,Integer size){
        Integer pageIndex = (page-1)*size;
        List<Comment> comments = commentMapper.findCommentByKeyWord1(key,pageIndex,size);
        List<Comment> commentList = new ArrayList<>();
        for (Comment comment:comments){
            comment.setEntertainmentId(entertainmentRepository.findEntertainmentByEntertainmentId(comment.getEntertainmentId()).getName());
            commentList.add(comment);
        }
        return commentList;
    }

    public List<Comment> findAllEntertainmentComments() {
        List<Comment> comments = commentMapper.findAllEntertainmentComments();
        List<Comment> commentList = new ArrayList<>();
        for (Comment comment:comments){
            comment.setEntertainmentId(entertainmentRepository.findEntertainmentByEntertainmentId(comment.getEntertainmentId()).getName());
            commentList.add(comment);
        }
        return commentList;
    }
    public List<Comment> findAllEntertainmentComments1(int page,int size) {
        int pageIndex = (page-1)*size;
        List<Comment> comments = commentMapper.findAllEntertainmentComments1(pageIndex,size);
        List<Comment> commentList = new ArrayList<>();
        for (Comment comment:comments){
            comment.setEntertainmentId(entertainmentRepository.findEntertainmentByEntertainmentId(comment.getEntertainmentId()).getName());
            commentList.add(comment);
        }
        return commentList;
    }

    public List<Comment> selectByEntertainment(String key) {
        List<Comment> comments = commentMapper.selectByEntertainment(key);
        List<Comment> commentList = new ArrayList<>();
        for (Comment comment:comments){
            comment.setEntertainmentId(entertainmentRepository.findEntertainmentByEntertainmentId(comment.getEntertainmentId()).getName());
            commentList.add(comment);
        }
        return comments;
    }


    public List<Comment> selectByEntertainment1(String key,Integer page,Integer size) {
        Integer pageIndex = (page-1)*size;
        List<Comment> comments = commentMapper.selectByEntertainment1(key,pageIndex,size);
        List<Comment> commentList = new ArrayList<>();
        for (Comment comment:comments){
            comment.setEntertainmentId(entertainmentRepository.findEntertainmentByEntertainmentId(comment.getEntertainmentId()).getName());
            commentList.add(comment);
        }
        return comments;
    }
}
